#ifndef CAMERACONTROLLER_H
#define CAMERACONTROLLER_H

#include "ecs.h"

struct CameraControllerComponent : public Component
{
    CameraControllerComponent(float zoomSpeed = 30.0f) : zoomSpeed(zoomSpeed) {}
    float zoomSpeed;
};

struct CameraController : public System
{
    CameraController(csugl::Ref<Scene> cur_scene) : System(cur_scene) {}
    void start() override
    {
        _cur_scene->ComponentEach<CameraComponent, CameraControllerComponent>(
            [](CameraComponent &camera_c, const CameraControllerComponent &camera_ct_c)
            {
                auto win = &csugl::singleton<csugl::Application>::getInstance()->GetWindow();
                win->addEventCallback(
                    [&](auto &ev)
                    {
                        csugl::EventDispatcher d(ev);
                        d.Dispatch<csugl::MouseScrolledEvent>(
                            [&](csugl::MouseScrolledEvent &ev)
                            {
                                auto camera = dynamic_cast<csugl::OrthographicCamera *>(camera_c.camera.get());
                                CHECK(nullptr != camera) << ": Camera is not a OrthographicCamera";
                                camera->zoom -= ev.vertical * csugl::LowpTime::deltaTime() * camera_ct_c.zoomSpeed;
                                return false;
                            });
                    });
            });
    }
};

#endif